// pages/login/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeTime:60,
    loginTime:3,
    phone:'',
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  codeTime(){
    var that = this;
    var codeTime = this.data.codeTime-1;
    if(codeTime>0){
      setTimeout(function(){
        that.setData({ codeTime })
        that.codeTime();
      },1000)
    }else{
      this.setData({ codeTime:60 })
    }
  },
  // 输入
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  codeInput(e) {
    console.log(e)
    this.setData({
      code: e.detail.value
    })
  },
  loginTimeFun(){
    var that =this;
    var loginTime = this.data.loginTime-1;
    if(loginTime>0){
      setTimeout(function () {
        that.setData({ loginTime })
        that.loginTimeFun();
      }, 1000)
    }else{
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  },
  // 绑定手机
  bindPhone() {
    var phone = this.data.phone, code = this.data.code;
    if (phone == '' || phone.length < 11) {
      wx.showToast({
        title: '请输入11位手机号',
        icon: 'none'
      })
    } else if (code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
    } else {
      getApp().request({
        url: '/login/sign',
        data: {
          phone, code
        },
        success(res) {
          console.log(res, '手机登录')
          if (res.data.code == 200) {
            this.setData({
              alertStatus: true
            })
            this.loginTimeFun()
          }
        }
      })
    }
  },
  // 获取验证码
  getCode() {
    var that = this
    var phone = this.data.phone, code = this.data.code;
    if (phone == '' || phone.length < 11) {
      wx.showToast({
        title: '请输入11位手机号',
        icon: 'none'
      })
    } else {
      // 验证码倒计时
      getApp().request({
        url: '/login/code/send',
        data: { phone },
        success(res) {
          console.log(res, '验证码---');
          if (res.data.code == 200) {
            wx.showToast({
              title: '获取验证码成功',
              icon: 'success'
            })
            that.codeTime();
          }
        }
      })
    }
  },
})